package net.zjitc.servlet;

import net.zjitc.domain.Book;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * Created by Administrator on 2017/10/15.
 */
@WebServlet(name = "CartServlet",urlPatterns = "/cartServlet")
public class CartServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out=response.getWriter();
        HttpSession session=request.getSession();
        if(session==null){
            out.write("购物车为空");
        }else {
            List<Book> cart= (List<Book>) session.getAttribute("cart");
            if(cart==null){
                out.write("购物车为空");
            }else {
                out.write("购物车商品如下：<br/>");
                for(Book b:cart){
                    out.write(b.getName()+"<br/>");
                }
            }
        }
    }
}
